package com.zzl.jedis;

import org.junit.Test;
import redis.clients.jedis.Jedis;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class demo1 {
    public static void main(String[] args) {
        //测试redis连接是否成功
        Jedis jedis = new Jedis("localhost",6379);
//        jedis.auth("123456");
        String he = jedis.ping();
        // value=PONG 连接成功
        System.out.println("value="+he);
        jedis.close();
    }

    /*操作Key*/
    @Test
    public void demo1(){
        Jedis jedis = new Jedis("localhost",6379);
//        jedis.auth("123456");
        jedis.set("k1","v1");
        jedis.set("k2","v2");
        jedis.set("k3","v3");
        // 模糊匹配
        Set<String> keys = jedis.keys("*");
        System.out.println(keys.size());
        for (String key:keys){
            System.out.println(key);
        }
        //判断键：k1是否有效
        System.out.println(jedis.exists("k1"));
        //查看键k2存在，但没有设置剩余生存时间
        System.out.println(jedis.ttl("k2"));
        //获取键k1
        System.out.println(jedis.get("k1"));
        jedis.close();
    }

    /*操作String*/
    @Test
    public void demo2(){
        Jedis jedis = new Jedis("localhost",6379);
//        jedis.auth("123456");
        jedis.mset("l1","k1","l2","v2","l3","v3");
        System.out.println(jedis.mget("l1","l2","l3"));
        jedis.close();
    }
    /*操作list*/
    @Test
    public void demo3(){
        Jedis jedis = new Jedis("localhost",6379);
//        jedis.auth("123456");
        jedis.lpush("mylist","redis","mysql","java");
        List<String> list = jedis.lrange("mylist",0,-1);
        for (String element : list) {
            System.out.println(element);
        }
        jedis.close();
    }

    /*操作hash*/
    @Test
    public void demo4(){
        Jedis jedis = new Jedis("localhost",6379);
//        jedis.auth("123456");
        jedis.hset("student","sname","zhangsan");
        String sname = jedis.hget("student", "sname");
        System.out.println(sname);

        Map<String,String> map = new HashMap<String,String>();
        map.put("telphone","15016580799");
        map.put("address","shanghai");
        map.put("email","abc@163.com");
        jedis.hmset("hash2",map);
        List<String> result = jedis.hmget("hash2", "telphone","email");
        for (String element : result) {
            System.out.println(element);
        }
        jedis.close();
    }

    /*操作Set*/
    @Test
    public void demo5(){
        Jedis jedis = new Jedis("localhost",6379);
//        jedis.auth("123456");
        //添加数据
        jedis.sadd("orders", "order01");
        jedis.sadd("orders", "order02");
        jedis.sadd("orders", "order03");
        jedis.sadd("orders", "order04");
        //获取数据
        Set<String> smembers = jedis.smembers("orders");
        for (String order : smembers) {
            System.out.println(order);
        }
        System.out.println();
        //删除数据
        jedis.srem("orders", "order02");
        Set<String> smembers2 = jedis.smembers("orders");
        for (String order : smembers2) {
            System.out.println(order);
        }
        jedis.close();
    }
    /*操作zset*/
    @Test
    public void demo6(){
        Jedis jedis = new Jedis("localhost",6379);
//        jedis.auth("123456");

        jedis.zadd("zset01", 100d, "z3");
        jedis.zadd("zset01", 90d, "l4");
        jedis.zadd("zset01", 80d, "w5");
        jedis.zadd("zset01", 70d, "z6");
        Set<String> zrange = jedis.zrange("zset01", 0, -1);
        for (String result : zrange){
            System.out.println(result);
        }
        jedis.close();
    }
}
